﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using TravelLink.DAL;
using TravelLink.WebModel;

namespace TravelLink.WebApp.Report {
    public partial class ReportENView : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {

                int resumeId = WebHelper.GetQueryStringInt("rid", 0);
                var db = DataContextFactory.Create();
                var resumeEntity = db.TL_Resume.Where(u => u.Idx == resumeId).FirstOrDefault();
                if (resumeEntity == null) return;
                if (resumeEntity.Language != 2) { Response.Redirect("ReportCNView.aspx?rid=" + resumeId); }
                Guid userCode = resumeEntity.UserCode;
                var userEntity = db.TL_User.Where(u => u.UserCode == userCode).FirstOrDefault();

                var CaeerExpData = db.TL_User_CareerExperience
                     .Where(u => u.ResumeId == resumeId)
                     .ToList().Select(u => new
                     {
                         Achievement = u.Achievement,
                         CompanyAddress = u.CompanyAddress,
                         CompanyName = u.CompanyName,
                         JobDesc = u.JobDesc,
                         FromYear = !u.FromYear.HasValue ? "" : u.FromYear.Value.ToString("yyyy/MM"),
                         CompanySite = u.CompanySite,
                         ContactMobile = u.ContactMobile,
                         ContactName = u.ContactName,
                         ContacttMail = u.ContacttMail,
                         LeaveReason = u.LeaveReason,
                         Position = u.Position,
                         ReportTo = u.ReportTo,
                         RowTime = DateTime.Now,
                         ToYear = !u.ToYear.HasValue ? "Now" : u.ToYear.Value.ToString("yyyy/MM")
                     })
                     .ToList();
                List<TL_User_Skills> skillData = db.TL_User_Awards.Where(u => u.ResumeId == resumeId).ToList().Select(u => new TL_User_Skills {
                    Skill = u.Description,
                    RowTime = u.RowTime

                }).ToList();

                List<TL_User_Qualification> shipData = db.TL_User_Qualification.Where(u => u.ResumeId == resumeId).ToList();

                List<NavItem> expdata = db.TL_User_Membership.Where(u => u.ResumeId == resumeId).Select(u => new
              NavItem {
                  Title = u.Name
              }).ToList();


                var EducationData = (from a in db.TL_User_Education
                                     join b in db.TL_Education
                                     on a.EducationTypeId equals b.Idx
                                     where a.ResumeId == resumeId
                                     select new
                                     {
                                         EducationType = b.Name,
                                         a.Course,
                                         a.FromYear,
                                         a.School,
                                         a.ToYear
                                     }).ToList();



                this.ReportViewer1.LocalReport.ReportPath = "EN_Resume2.rdlc";

                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Name", string.Concat(userEntity.CFirstName, userEntity.CLastName)));
                //this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("date1", userEntity.BirthDay.HasValue ? userEntity.BirthDay.Value.ToString("yyyy/MM/dd") : ""));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Tempdate", userEntity.BirthDay.HasValue ? userEntity.BirthDay.Value.ToString("yyyy/MM/dd") : ""));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Gender", (userEntity.Gender.HasValue && userEntity.Gender.Value) ? "Male" : "Female"));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Email", userEntity.Email));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Mobile", userEntity.Mobile));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Address", resumeEntity.Address));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Tel", userEntity.Tel));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Target", Adapter(resumeEntity.Objective)));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("IsMarry", (userEntity.IsMarried.HasValue && userEntity.IsMarried.Value) ? "Married" : "Single"));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Intestting", Adapter(resumeEntity.Interests)));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("ZipCOde", Adapter(resumeEntity.ExpectedWorkin)));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Profile", Adapter(resumeEntity.Profile)));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("LanguageDesc", Adapter(resumeEntity.LanguageDesc)));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("EName", string.Concat(userEntity.EFirstName, userEntity.ELastName)));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Country", userEntity.TL_Country != null ? userEntity.TL_Country.ECountry : " "));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Zip", userEntity.PostCode));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("Status", resumeEntity.Status));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("NoticePeriod", resumeEntity.NoticePerod));
                string birthCity = string.Empty;
                string placeCity = string.Empty;
                if (resumeEntity.BirthCity.HasValue) {
                    birthCity = db.TL_City.Where(u => u.Idx == (resumeEntity.BirthCity ?? 0)).Select(u => u.CName).FirstOrDefault();
                }

                if (resumeEntity.PlaceCity.HasValue) {
                    placeCity = db.TL_City.Where(u => u.Idx == (resumeEntity.PlaceCity ?? 0)).Select(u => u.CName).FirstOrDefault();
                }
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("BirthCity", birthCity));
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("PlaceCity", placeCity));

                //this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", null));
                //this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet3", null));
                //this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet4", null));
                //this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet5", null));


                this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("SkillData", skillData));


                this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("QuailData", shipData));


                this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("ExpData", expdata));


                this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("EducationData", EducationData));



                //List<TL_User_CareerExperience> CaeerExpData = new List<TL_User_CareerExperience>();
                //CaeerExpData.Add(new TL_User_CareerExperience {
                //    Achievement = "asd as das da sda sd a",
                //    CompanyAddress = "sdasd as das ad s",
                //    CompanyName = "asdasd",
                //    CompanySite = "http://www.baidu.com",
                //    ContactMobile = "123123",
                //    ContactName = "ffgg",
                //    ContacttMail = "itwujunjie@amil.com",
                //    FromYear = DateTime.Now.AddDays(1),
                //    Idx = 1,
                //    JobDesc = "dasdasd asd a sda sd asd as d as d",
                //    ResumeId = 1,
                //    LeaveReason = "asdas das da sda",
                //    Position = "asdas",
                //    ReportTo = "dd",
                //    RowTime = DateTime.Now,
                //    ToYear = DateTime.Now.AddHours(1)
                //});
                this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("CaeerExpData", CaeerExpData));

                byte[] reportViewer1LocalReportRender = this.ReportViewer1.LocalReport.Render("Word");

                Response.OutputStream.Write(reportViewer1LocalReportRender, 0, reportViewer1LocalReportRender.Length);
                Response.ContentType = "application/msword";
                Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(Guid.NewGuid().ToString() + "_EN.doc", System.Text.Encoding.UTF8));

                Response.End();
            }

        }
        public string Adapter(string obj) {
            if (string.IsNullOrEmpty(obj)) {
                return "Empty";
            }
            if (string.IsNullOrEmpty(obj.Trim())) {
                return "Empty";
            }
            return obj.ToString();
        }
    }
}